package code.tools.commponents
{
	import flash.utils.Dictionary;
	
	/**
	 *
	 *@author wenqiang.deng
	 *Create time:2013-8-22 下午4:02:52
	 */
	public class AdaptTfGroup
	{
		//TODO dwq 
		public static var activityGroup:AdaptTfGroup = new AdaptTfGroup();
		//
		private var _adaptSizeDic:Dictionary;
		private var _list:Vector.<AutoTextField>;
		public function AdaptTfGroup()
		{
			_list = new Vector.<AutoTextField>();
			_adaptSizeDic = new Dictionary();
		}
		
		/**
		 * 添加新的自适应文本框 
		 * @param tf
		 * 
		 */		
		public function appendTf(tf:AutoTextField):AdaptTfGroup
		{
			tf.setOnZoom(tfZoomHandler);
			_adaptSizeDic[tf] = tf.defaultTextSize;
			_list.push(tf);
			return this;
		}
		
		/**
		 * 移除自适应文本框 
		 * @param tf
		 * 
		 */		
		public function removeTf(tf:AutoTextField):void
		{
			if(_list.indexOf(tf)!=-1)
			{
				tf.setOnZoom(null);
				delete _adaptSizeDic[tf];
				_list.splice(_list.indexOf(tf),1);
			}
		}
		
		private function tfZoomHandler(tf:AutoTextField,size:int):void
		{
			_adaptSizeDic[tf] = size;
			var minSize:int = size;
			for each(var value:int in _adaptSizeDic)
			{
				if(minSize > value)
					minSize = value;
			}
			synchroTextSize(minSize);
		}
		
		/**
		 * 组内的文本框同步尺寸 
		 * 
		 */		
		private function synchroTextSize(size:int):void
		{
			for(var i:int = 0;i < _list.length;i++)
				_list[i].synchroSize(size);
		}
	}
}